package com.carlyang.studentinfo.web;

import java.io.IOException;
import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.carlyang.studentinfo.dao.ClassDao;
import com.carlyang.studentinfo.util.DBUtil;
import com.carlyang.studentinfo.util.LogUtil;
import com.carlyang.studentinfo.util.ResponseUtil;

import net.sf.json.JSONObject;

public class ClassDeleteServlet extends HttpServlet{
	public static final String TAG = "ClassDeleteServlet";
	ClassDao classDao = new ClassDao();
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String delIds = request.getParameter("delIds");
		Connection connection=null;
		try {
			LogUtil.i(TAG, "连接数据库......");
			connection = DBUtil.getConnection();
			JSONObject jsonObject = new JSONObject();
			int delNums = classDao.deleteClass(connection, delIds);
			if (delNums>0) {
				jsonObject.put("success", "true");
				jsonObject.put("delNums", delNums);
				//System.out.println("删除 第"+delIds+"项 成功！");
				LogUtil.i(TAG, "删除 第"+delIds+"项 成功！");
			}else {
				jsonObject.put("errorMsg", "删除失败！");
				//System.out.println("删除 第"+delIds+"项 失败！");
				LogUtil.i(TAG, "删除 第"+delIds+"项 失败！");
			}
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			try {
				DBUtil.closeCon(connection);
				LogUtil.i(TAG, "关闭数据库连接成功！---------------------------------");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
}
